const HttpException = require('../core/httpException.js')

module.exports = async (ctx, next) => {
	ctx.HttpException = HttpException
	try {
		await next()
	} catch (err) {
		if (process.env.ISDEV) {
			console.log(err)
		}

		if (err instanceof HttpException) {
			ctx.status = err.status
			ctx.body = {
				msg: err.msg,
				errorCode: err.errorCode
			}
		} else {
			ctx.status = 500
			ctx.body = {
				msg: 'unknown error',
				error_code: 5000
			}
		}
	}
}
